#include <iostream>
using namespace std;

int** init(int* count) {
 int v_count;
 char* str = new char();
 int ** Euler;

 cout<<"Input the Vertexs' count: "<<endl;
 cin>>v_count;
 *count = v_count;
 Euler = (int **) malloc (sizeof(int *) * v_count);
 for (int i = 0; i < v_count; i++) {
  Euler[i] = (int *) malloc (sizeof(int) * v_count);
  memset(Euler[i], 0, sizeof(int) * v_count);
 }
 do {
  int v_1, v_2;
  cout<<"Input a Edge (ex. 2,3 or \'exit\' to exit): "<<endl;
  cin>>str;
  if (strcmp(str, "exit") == 0) {
   break; 
  }
  sscanf(str, "%d,%d", &v_1, &v_2);
  if (v_1 < 0 || v_1 >= v_count || v_2 < 0 || v_2 >= v_count) {
   fprintf(stderr, "Error : must be in (%d~%d)\n", 0, v_count - 1);
   continue;
  }
  Euler[v_1][v_2] = Euler[v_2][v_1] = 1;
 } while(true);

 return Euler;
}

bool isEulerCircle(int** Euler, int count) {
 int i, j, degree;
 
 for(i = 0; i < count; i++) {
  degree = 0;
  for(j = 0; j < count; j++) {
   degree+=Euler[i][j]; 
  }
  if(degree % 2 == 1)
   return false;
 }
 return true;
}

void Fleury(int** Euler, int count) {
 int i, j, v, flg = 1, large = 0;

 for(i = 0; i < count; i++) {
  int tmp = 0;
  for(j = 0; j < count; j++) {
   tmp += Euler[i][j];
  }
  if (tmp > large)
   v = i;
 }
 while(flg) {
  flg = 0;
  for(i = 0; i < count; i++) {
   if(Euler[v][i] == 1) {
    Euler[v][i] = 0;
    Euler[i][v] = 0;
    flg = 1;
    printf("From %d to %d\n", v, i);
    v = i;
   }
  }
 }
}

void main(){
 int ** Euler;
 int count;

 Euler = init(&count);
 if(!isEulerCircle(Euler, count)) {
  printf("The Euler Circle is wrong!\n");
  exit(0);
 }
 Fleury(Euler, count);
} 